const {
    arrayToMap,
    CombinedPredicate: {
        not,
    },
    stringValidator: {
        isFloat,
    },
} = rx
const validate = RxFormValidator.validator({
    rxElemValidators: [
        RxElemValidator.notBlank('keyword', '[关键词] 不能为空'),
        RxElemValidator.notBlank('status', '[状态] 不能为空'),
        RxElemValidator.notBlank('weight', '[权重] 不能为空'),
        RxElemValidator.notBlank('matchCount', '[匹配次数] 不能为空'),
        RxElemValidator.notBlank('category', '[关键词组] 不能为空'),

        RxElemValidator.limitLen('keyword', '[关键词] 长度最大为 50 位', 50),
        RxElemValidator.limitLen('weight', '[权重] 长度最大为 9 位', 9),
        RxElemValidator.limitLen('matchCount', '[匹配次数] 长度最大为 15 位', 15),

        RxElemValidator.create('weight', '[权重] 必须为数字', not(isFloat)),
        RxElemValidator.create('matchCount', '[匹配次数] 必须为数字', not(isFloat)),
    ],
})


const app = new BasicTableVue({
    data() {
        return {
            api: sysKeywordRepoApi,
            formData: this.createForm(),
            dialogShow: false,
            dtitle: '',
            timeArray: [],
            formLabelWidth: '70px',
            columns: [
                new TableColumn({width: 30, type: 'selection'}),
                new TableColumn({prop: 'keyword', title: '关键词'}),
                new TableColumn({prop: 'status', title: '状态', customComponent: true}),
                TableColumn.generate({prop: 'weight', title: '权重'}),
                TableColumn.generate({prop: 'matchCount', title: '匹配次数'}),
                new TableColumn({prop: 'categoryName', title: '关键词组'}),
                TableColumn.generate({
                    prop: 'createTime', title: '创建时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({prop: 'operating', title: '操作', customComponent: true}),

            ],
            statusImgList: {'1': 'yes.gif', '2': 'no.gif'},
            categoryMap: new Map(),
        }
    },
    methods: {
        createForm() {
            return new SysKeywordRepo({
                id: '',
                keyword: '',
                status: 1,
                weight: '',
                matchCount: '',
                createTime: '',
                category: '',
                delFlag: '0',
                createTimeRange: [],
                weightAction: null,
                matchCountAction: null,
                keywordAction: null,
            })
        },
        statusImg(val) {
            return '/static/img/' + this.statusImgList[val]
        },
        addInfo() {
            this.dtitle = '新增'
            this.dialogShow = true
            this.formData = this.createForm()
        },
        updateInfo(row) {
            this.dtitle = '修改'
            row.category = parseInt(row.category)
            this.formData = _.cloneDeep(row)
            this.dialogShow = true
        },
        async updateOrAddData() {
            if (!(await validate(this.formData))) {
                return
            }
            const res = await sysKeywordRepoApi.updateOrAdd(this.formData)
            if (!res || res.data !== true) {
                await rxPrompt.dangerMsg(this.dtitle + '失败')
                return
            }
            await Promise.all([
                await rxPrompt.successMsg(this.dtitle + '成功'),
                await this.searchPage(),
            ])
            this.dialogShow = false
        },
        async del(row) {
            const id = row.id
            if (id != null && id !== '') {
                const res = await rxPrompt.confirm('你确认要删除该记录吗?？', ['确认', '取消'])
                if (res) {
                    const result = await this.api.deleteById(id)
                    // noinspection JSIgnoredPromiseFromCall
                    if (result) {
                        await Promise.all([
                            rxPrompt.successMsg('删除成功'),
                            this.searchPage(),
                        ])
                    }
                }
            } else {
                await rxPrompt.msg('操作失败')
            }

        },
        async initCategoryList() {
            //获取关键词类型列表
            this.categoryMap = arrayToMap(await sysBaseCodeApi.selectListByType(13), ({id}) => id, ({name}) => name)
        },
        async init() {
            await this.initCategoryList()
        },
        async downloadTemplate() {
            await sysKeywordRepoApi.downloadTemplate('关键字导入模板.xlsx')
        },

    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },

})
